﻿Imports System.Data.OleDb


Public Class VideosPlayComments
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then


        Else
            Response.Redirect("Videos.aspx")
        End If






        Dim sql3 As String = "INSERT INTO Video_Views_Stats (VideoID,DateViewed,TimeViewed) VALUES (@f1,@f2,@f3)"
        Dim conn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        conn3.Open()

        Dim cmd3 As OleDbCommand = New OleDbCommand(sql3, conn3)

        cmd3.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd3.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
        cmd3.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))

        cmd3.ExecuteNonQuery()
        conn3.Close()
        cmd3.Dispose()
        conn3.Dispose()



        Dim sql4 As String = "SELECT * FROM VideoView WHERE VideoID=@f20"

        Dim conn4 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn4.Open()

        Dim cmd4 As OleDbCommand = New OleDbCommand(sql4, conn4)

        cmd4.Parameters.AddWithValue("@f20", Request.Params("id"))

        Dim datareader2 = cmd4.ExecuteReader()


        datareader2.Read()

        If Not IsDBNull(datareader2("CountOfID")) Then lblTotalViews.Text = datareader2("CountOfID")


























        imgLike.Visible = False

        Dim sql As String = "SELECT * FROM Video_Likes WHERE UserID = @f1 AND VideoID = @f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f2", Request.Params("id"))

        conn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        If dr.HasRows Then    'If there are 1 or more rows which are returned then hide the button and show the already liked image (thumbs up) otherwise hide the liked image and show the Like Button. 
            imgLike.Visible = True
            btnLike.Visible = False


        Else

            imgLike.Visible = False
            btnLike.Visible = True


        End If

        cmd.Dispose()
        conn.Close()
        conn.Dispose()

        Dim sql2 As String = "SELECT * FROM VideoLikesQuery WHERE VideoID=@f20"

        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn2.Open()

        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)

        cmd2.Parameters.AddWithValue("@f20", Request.Params("id"))

        Dim datareader = cmd2.ExecuteReader()


        datareader.Read()

        If Not IsDBNull(datareader("CountOfID")) Then lblTotalCounts.Text = datareader("CountOfID")

    End Sub



    Protected Sub btnAddComment_Click(sender As Object, e As EventArgs) Handles btnAddComment.Click

        If txtAddVideoComment.Text = "" Then
            MsgBox("You have not entered a comment for this video, please check and try again")
        Else

            Dim sql As String = "INSERT INTO Video_Comments (CommentBody,DateUploaded,TimeUploaded,UserID,VideoID) VALUES (@f1,@f2,@f3,@f4,@f5)"

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

            conn.Open()

            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", txtAddVideoComment.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("id"))

            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()




            Response.Redirect("VideosPlayComments.aspx?id=" & Request.QueryString("id"))
        End If














    End Sub

    Protected Sub btnLike_Click(sender As Object, e As EventArgs) Handles btnLike.Click
        imgLike.Visible = True
        btnLike.Visible = False

        Dim sql As String = "INSERT INTO Video_Likes (VideoID,UserID) VALUES (@f1,@f2)"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("id"))
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)

        conn.Open()

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("VideosPlayComments.aspx?id=" & Request.QueryString("id"))
    End Sub
End Class